﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lottery.Entity;
using Lottery.DBUtility;
using System.Data.SqlClient;
using System.Data;


namespace Lottery.DataAccess
{
    public class LotteryIssueController
    {
        private const string SELECT_ALLISSUE = "SELECT * FROM LotteryIssue ";
        private const string CONDITION_BYISSUENUMBER = " WHERE IssueNumber = " + PARM_ISSUENUMBER;
        private const string INSERT = "INSERT INTO LotteryIssue(IssueDate, IssueNumber) VALUES(" + 
                                            PARM_ISSUEDATE + ","+ PARM_ISSUENUMBER + ")";
        private const string ORDERBY_ISSUENUMBER = " ORDER BY IssueNumber";

        private const string PARM_ISSUEDATE = "@IssueDate";
        private const string PARM_ISSUENUMBER = "@IssueNumber";

        public LotteryIssue Insert(LotteryIssue lotteryIssue)
        {
            LotteryIssue result = GetIssueByNumber(lotteryIssue.IssueNumber);

            if (result == null)
            {
                LotterySQLHelper.ExecuteNonQuery(CommandType.Text, INSERT, new SqlParameter(PARM_ISSUEDATE, lotteryIssue.IssueDate),
                                                      new SqlParameter(PARM_ISSUENUMBER, lotteryIssue.IssueNumber));
                result = GetIssueByNumber(lotteryIssue.IssueNumber);
            }
            return result;
        }

        public LotteryIssue GetIssueByNumber(int issueNumber)
        {
            return LotterySQLEntityHelper<LotteryIssue>.GetEntity(SELECT_ALLISSUE + CONDITION_BYISSUENUMBER,
                   new SqlParameter(PARM_ISSUENUMBER, issueNumber));
        }

        public List<LotteryIssue> GetIssueList()
        {
            return LotterySQLEntityHelper<LotteryIssue>.GetEntitySet(SELECT_ALLISSUE + ORDERBY_ISSUENUMBER);
        }
    }
}
